package ics314;

public class Vor {  // This is where most of the code is for now.
  
  int obs;
  int inverseOBS;  // This is just 180 degrees off the OBS.
  RadioStation station;
  int radial;
  
  public Vor (int desired, RadioStation r) {
    
    obs = desired;
    station = r;
    radial = station.getRadial();
    
    if (obs == 360) {
      
      obs = 0;
      
    }
    
    if (obs >= 180) {
      
      inverseOBS = obs - 180;
      
    } else {
      
      inverseOBS = obs + 180;
      
    }
    
  }
  
  public int getObs() {
	  return this.obs;
  }
  
  public int getRadial() {
	  return this.radial;
  }
  
  public void setObs(int obs) {
	  this.obs = obs;
	  this.inverseOBS = (obs + 180) % 360; 
  }
  
  public void setRadial(int rad) {
	  this.radial = rad;
  }
  
  public void setRadioStation(RadioStation r) {
	  this.station = r;
  }
  
  public String getRadioStationID() {
	  return station.getID();
  }
  
  public boolean isGood() {  // Here it is checking the booleans for if the signal is good.
    
    boolean good = true;
    
    if (station.isTooFarAway()) {
      
      good = false;
      return good;
      
    } else if ((Math.abs(obs - radial) == 90) || (Math.abs(obs - radial) == 270)) {
      // This checks if the plane is 90 degrees off the OBS
      good = false;
      return good;
      
    } else if (station.isDirectlyOver()) {
      
      good = false;
      return good;
      
    } else {
          
    return good;
    
    }
    
  }
  
  public boolean isGoingTo() { // This part checks if the plane is either going toward or away from the station.
    // We need to implement a check if the signal is bad before running this math.
    boolean to = false;
    
    if ((Math.abs(obs - radial) > 90) && (Math.abs(obs - radial) < 270)) {
      
      to = true;
      
    }
    
    return to;
    
  }
  
  public int getDegreesOff() { // This method calculates the degrees off the radial is from the OBS.
    // Again we need to put in a bad signal check.  Other than that, the math works out.
    // Because the VOR only needs to know if the radial is between -10 and 10 degrees, I made it only return values in that range.
    int degrees;
    
    if (radial == obs || radial == inverseOBS) {
      
      // The case where the plane is flying right on the intended radial.
      degrees = 0;
      return degrees;
      
    }
   
    if (isGoingTo()) { // The plane is flying toward the radio station.  It is closer to the OBS' reciprocal.
      
      if ((inverseOBS >= 90) && (inverseOBS <= 270)) { // The OBS' reciprocal 90 to 270 degrees.
      
        degrees = inverseOBS - radial;
      
        if (degrees >= 10) {
        
          degrees = 10;
        
        } else if (degrees <= -10) {
        
          degrees = -10;
      
        }
        
        return -degrees;
          
      } else if (inverseOBS < 90) { // The OBS' reciprocal is 0 to 89 degrees.
        
        if (radial > 270) { // The radial is 271 to 359 degrees.
        
          degrees = inverseOBS + (360 - radial);
        
        } else { // The radial is 0 to 179 degrees.
          
         degrees = inverseOBS - radial;
          
        } if (degrees >= 10) {
          
          degrees = 10;
        
        } else if (degrees <= -10) {
        
          degrees = -10;
      
        }
        
        return -degrees;
        
      } else { // The OBS' reciprocal is 271 to 359 degrees.
        
        if (radial < 90) { // The radial is between 0 and 89 degrees.
          
          degrees = -((360 - inverseOBS) + radial);
          
        } else { // The radial is between 181 and 359 degrees.
          
          degrees = inverseOBS - radial;
          
        } if (degrees >= 10) {
        
          degrees = 10;
        
        } else if (degrees <= -10) {
        
          degrees = -10;
      
        }
        
        return -degrees;
        
      }
    
    } else { // The plane is going from the radio station.  It is closer to the OBS.
      
      if ((obs >= 90) && (obs <= 270)) { // The OBS is from 90 to 270 degrees.
        
        degrees = radial - obs;
      
        if (degrees >= 10) {
        
          degrees = 10;
        
        } else if (degrees <= -10) {
        
          degrees = -10;
      
        }
        
        return -degrees;
          
      } else if (obs < 90) { // The OBS is from 0 to 89 degrees.
        
        if (radial > 270) { // The radial is from 271 to 359 degrees.
        
          degrees = -(obs + (360 - radial));
        
        } else { // The radial is from 1 to 179 degrees
          
         degrees =  radial - obs;
          
        } if (degrees >= 10) {
          
          degrees = 10;
        
        } else if (degrees <= -10) {
        
          degrees = -10;
      
        }
        
        return -degrees;
        
      } else { // The OBS is from 271 to 359 degrees.
        
        if (radial < 90) { // The radial is from 0 to 89 degrees.
          
          degrees = (360 - obs) + radial;
          
        } else { // The radial is from 181 to 359 degrees.
          
          degrees = radial - obs;
          
        } if (degrees >= 10) {
        
          degrees = 10;
        
        } else if (degrees <= -10) {
        
          degrees = -10;
      
        }
        
        return -degrees;
        
      }
      
    }
    
  }

}
